// pages/ledger/day/index.js
var fileManager = require('../../../libs/fileManager');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		list: [
			// {
			// 	id: 1,
			// 	datetime: "2020-20-20 20:20:20",
			// 	time: "20:20:20",
			// 	purpose: "买烟黄鹤楼",
			// 	money: 17,
			// 	author: "小李同学",
			// }
		],
		totalMoney: 0,
		fileMonth: "",
		fileDayFileName: ""
	},
	onPullDownRefresh() {
		this.getList(this.data.fileDayFileName, this.data.fileMonth)
		setTimeout(() => {
			wx.stopPullDownRefresh()
		}, 500);
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		console.log("日账单参数", options)
		if (options.file && options.month) {
			this.setData({
				fileMonth: options.month,
				fileDayFileName: options.file
			})
			return this.getList(options.file, options.month)
		}
		this.getList()
	},
	getList(file, month) {
		fileManager.getTodayDataJS(file, month).then(res => {
			this.setData({
				totalMoney: 0
			})
			res.data.forEach(item => {
				this.setData({
					totalMoney: Number(this.data.totalMoney) + Number(item.money)
				})
			})
			this.setData({
				list: [...res.data]
			})
		})
	},
	back() {
		wx.navigateBack()
	},

	useOption(e) {
		wx.showActionSheet({
			itemList: ["删除", "详情"],
			success: (res) => {
				if (res.tapIndex === 1) {
					this.getDetail(e)
				}

				if (res.tapIndex === 0) {
					this.removeData(e)
				}
			}
		})
	},

	getDetail(e) {
		let item = e.currentTarget.dataset.item
		console.log("入账明细", item)
		wx.showModal({
			title: `${item.time}消费明细`,
			content: `${item.authorLabel}【${item.purpose}】，本次消费${item.money}元`,
			showCancel: false,
			confirmText: "团团收到",
			confirmColor: "#0fc75b",
			complete: (res) => {
				// console.log(res)
			}
		})
	},
	removeData(e) {
		let item = e.currentTarget.dataset.item
		fileManager.removeDayItemDataJS(item, this.data.fileMonth, this.data.fileDayFileName).then(res => {
			this.getList(this.data.fileDayFileName, this.data.fileMonth)
		})
	}
})